﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uk.ac.soton.ses.DAL;
using System.IO;

namespace uk.ac.soton.ses
{
    public static class MaterialsBusinessObjectsExtensions
    {
        public static Experiment CopyToDB(this ExperimentBO experimentBO)
        {
            Experiment experimentDB = new Experiment();

            experimentDB.Name = experimentBO.Name;
            experimentDB.Owner = 0; //Owner is not set by us here (at least not yet), ownership will need be assigned in the interface
            experimentDB.Date = experimentBO.Date;
            experimentDB.Description = experimentBO.Description;
            experimentDB.ExperimentTypeID = experimentBO.ExperimentTypeID;
            experimentDB.DefaultBasePath = experimentBO.DefaultBasePath;
            experimentDB.IsDeleted = experimentBO.IsDeleted;
            experimentDB.FileSystemUserID = experimentBO.FileSystemUserID;
            experimentDB.FileStoreOptionsExperimentID = HDCConfiguration.Instance.ConfigurationOptions.FileSystemWatcherOptions.ConfigurationContainerID;

            return experimentDB;
        }

        public static void CopyToDB(this ExperimentDataFileBO experimentDataFileBO, ExperimentDataFile dataFileDB)
        {
            dataFileDB.Filename = experimentDataFileBO.RemainingPath;
            dataFileDB.Description = experimentDataFileBO.Description;
            dataFileDB.BasePath = experimentDataFileBO.BasePath;
            dataFileDB.LastWriteTime = experimentDataFileBO.LastWriteTime;
            dataFileDB.CreationTime = experimentDataFileBO.CreationTime;
            dataFileDB.LastAccessTime = experimentDataFileBO.LastAccessTime;
            dataFileDB.FileSize = experimentDataFileBO.FileSize;
            dataFileDB.SHA1Hash = experimentDataFileBO.SHA1Hash;
            dataFileDB.SHA1HashGeneratedTime = experimentDataFileBO.SHA1HashGeneratedTime;
            dataFileDB.IsDeleted = experimentDataFileBO.IsDeleted;
            dataFileDB.FileSystemUserID = experimentDataFileBO.FileSystemUserID;
            //Owner is not set by us here (at least not yet), ownership will need be assigned in the interface
            //if (dataFileDB.Owner == null) dataFileDB.Owner = 0;
            dataFileDB.ExperimentDataFileTypeID = experimentDataFileBO.ExperimentDatatypeID;
        }

        public static ExperimentDataFile CopyToDB(this ExperimentDataFileBO experimentDataFileBO)
        {
            ExperimentDataFile dataFileDB = new ExperimentDataFile();
            experimentDataFileBO.CopyToDB(dataFileDB);
            return dataFileDB;
        }

        public static ExperimentBO Copy(this ExperimentBO experimentBO)
        {
            ExperimentBO newExperimentBO = new ExperimentBO
            {
                ID=experimentBO.ID,
                Name = experimentBO.Name,
                //OwnerID = experimentBO.OwnerID,
                Date = experimentBO.Date,
                Description = experimentBO.Description,
                ExperimentTypeID = experimentBO.ExperimentTypeID,
                DefaultBasePath = experimentBO.DefaultBasePath,
                IsDeleted = experimentBO.IsDeleted,
                RowVersion=experimentBO.RowVersion,
                FileSystemUserID=experimentBO.FileSystemUserID
            };
            return newExperimentBO;
        }

        public static ExperimentDataFileBO Copy(this ExperimentDataFileBO experimentDataFileBO)
        {
            ExperimentDataFileBO newExperimentDataFileBO = new ExperimentDataFileBO
            {
                ID=experimentDataFileBO.ID,
                RemainingPath = experimentDataFileBO.RemainingPath,
                Description = experimentDataFileBO.Description,
                BasePath = experimentDataFileBO.BasePath,
                LastWriteTime = experimentDataFileBO.LastWriteTime,
                CreationTime = experimentDataFileBO.CreationTime,
                LastAccessTime = experimentDataFileBO.LastAccessTime,
                FileSize = experimentDataFileBO.FileSize,
                SHA1Hash = experimentDataFileBO.SHA1Hash,
                SHA1HashGeneratedTime = experimentDataFileBO.SHA1HashGeneratedTime,
                IsDeleted = experimentDataFileBO.IsDeleted,
                RowVersion=experimentDataFileBO.RowVersion,
                FileSystemUserID = experimentDataFileBO.FileSystemUserID,
                ExperimentID = experimentDataFileBO.ExperimentID,
                ExperimentDatatypeID = experimentDataFileBO.ExperimentDatatypeID
            };
            return newExperimentDataFileBO;
        }

        public static string GetFullPath(this ExperimentDataFileBO datafile)
        {
            return Path.Combine(datafile.BasePath, datafile.RemainingPath);
        }

        public static List<ExperimentDataFileBO> MatchingDataFiles(this List<ExperimentDataFileBO> listOfFiles, ExperimentDataFileBO fileToMatch)
        {
            List<ExperimentDataFileBO> listOfMatchingFiles = new List<ExperimentDataFileBO>();

            foreach (ExperimentDataFileBO file in listOfMatchingFiles)
            {
                if (file.Matches(fileToMatch))
                {
                    listOfMatchingFiles.Add(file);
                }
            }
            return listOfMatchingFiles;

        }

    }
}
